package dao;

import jdbc.JDBCUtils;
import jdbc.SQL;
import entity.Customer;

import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao{


    SQL sql = SQL.getSQL();
    JDBCUtils jdbcUtils = JDBCUtils.getJDBCUtils();

    @Override
    public int addCustomer(Customer customer) {
        String addSql = sql.getAddSql();
        Object[] obj = {customer.getId(),customer.getName(),customer.getGender(),customer.getAge(),customer.getPhone(),customer.getEmail()};
        int addCount = jdbcUtils.updateCustomer(addSql, obj);
        return addCount;
    }

    @Override
    public ArrayList<Customer> findAllCustomer() {
        String allCustomer = sql.getFindAllCustomer();
        List<Customer> list = jdbcUtils.getList(Customer.class, allCustomer);

        return (ArrayList<Customer>) list;
    }

    @Override
    public int updateCustomer(Customer customer) {

        String updateSql = sql.getUpdateSql();
        Object[] obj = {customer.getName(),customer.getGender(),customer.getAge(),customer.getPhone(),customer.getEmail(),customer.getId()};
        int updateCount = jdbcUtils.updateCustomer(updateSql, obj);

        return updateCount;

    }

    @Override
    public Customer findOneCustomer(int id) {
        String findOneCustomer = sql.getFindOneCustomer();
        Customer customer = jdbcUtils.getCustomer(findOneCustomer, id);
        return customer;
    }

    @Override
    public int deleteCustomer(Customer customer) {
        String deleteSql = sql.getDeleteSql();

        int deleteCount = jdbcUtils.updateCustomer(deleteSql, customer.getId());

        return deleteCount;
    }

    @Override
    public ArrayList<Customer> findAnyCustomer(int startIndex, int length) {

        List<Customer> list = jdbcUtils.getListAnyCustomer(sql.getFindAnyCustomer(), startIndex, length);

        return  (ArrayList<Customer>) list;
    }

}
